<?php
class _index
{
	public function initc()
	{
	    View::preshow('public/discuss/header');
	    View::endshow('public/discuss/footer');
	}
    
    /**
     * 话题列表
     */
    public function index()
    {
        View::$arrTplVar['list'] = Discuss::link('opinion')
            ->where(['status' => Discuss::STATUS_OK ])
            ->select()
            ->getAll();
        
        View::show('topic_list');
    }
    
    /**
     * 添加话题
     * 单独的添加页面
     */
    public function addTopic()
    {
    	if (Request::isPost()) {
    		$content = Request::Post('content', Safe::$Check_Empty);
    		$tag = Request::Post('tag', '');
    		
    		$id = Discuss::getGlobalId(Discuss::PROJECT);

    		$topic = [
                'id'       => $id,
                'content'  => $content,
                'tag'      => $tag,
                'status'   => Discuss::STATUS_INIT,
                'add_day' => REQUEST_DAY,
			];
            Discuss::link('opinion')->insert($topic)->query();
    		Response::redirect('添加成功, 审核通过后才能显示', Fun::buildUrl('discuss'));
		}
        View::show('topic_add');
    }
    
    /**
     * 喜欢, 点赞
     */
    public function likeit()
    {
        $rowId = Request::Route('row_id');
    
        $affectRows = 0;
        if (!empty($rowId)) {
            $affectRows = Note::link('note')
                ->where(['id' => $rowId])
                ->updateInc([['agree', 'agree', 1]])
                ->update()
                ->query()
                ->affectRows;
        }
        
        Response::success($affectRows, Note::$currentSql);
    }
    
    /**
     * 添加回复, 到待审核表
     * 添加页的HTML表单在话题详情页的模板中
     */
    public function addReview()
    {
        if (Request::isPost()) {
            $rootId = Request::Post('rootid', Safe::$Check_Empty);
            $fatherId = Request::Post('fatherid', 0);
            $content = Request::Post('content');
            
            $replayId = Discuss::getGlobalId(Discuss::PROJECT);
    
            Discuss::link('review')
                ->insert([
                    'id'       => $replayId,
                    'status'   => Discuss::STATUS_REPLAY_INIT,
                    'rootid'   => $rootId,
                    'fatherid' => $fatherId,
                    'content'  => $content,
                    'add_day'  => REQUEST_DAY,
                ])
                ->query();
            
            Response::success();
        } else {
            Response::error('请求错误');
        }
        
    }
    
    /**
     * 话题详情页
     */
    public function topicDetail()
    {
        $id = Request::Route('id');
        $detail = Discuss::link('opinion')
            ->where(['id' => $id])
            ->getOne();
        View::$arrTplVar['detail'] = $detail;
        View::show('topic_detail');
    }
    
    /**
     * 获取所有评论
     */
    public function getAllReplay()
    {
        $rootId= Request::Route('rootId');
        
        $tree = Discuss::getAllReplay($rootId);
        
        Response::success($tree);
    }
    
}